package com.gebiafu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;



/**
 * title: MySecurityConfig
 * author: Gebiafu
 * date: 2021/07/02 11:17
 */
@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置登陆表单
        http.formLogin()
                .loginPage("/")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/toMain");
        //配置登陆权限
        http.authorizeRequests()
                .antMatchers("/","/default","/login","/welcome","/toRegister","/register").anonymous()
                .anyRequest().authenticated();
        //配置异常
        http.exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler);

        http.csrf().disable();
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return  new BCryptPasswordEncoder();
    }
}
